﻿using System;
using System.Data;
using System.Text;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BugManager;

namespace GUI.Controls.News
{
    public partial class FormalizedList : System.Web.UI.UserControl
    {
        private const int ImageWidth = 196;//210;
        private const int ImageHeight = 100;//158;
        private const int ImageWidth2 = 196;//210;
        private const int ImageHeight2 = 196;//158;
        private const int TopFormalizedItem = 6;

        internal bool DataBind(DataTable dt)
        {
            try
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    var sb = new StringBuilder();
                    for (var i = 0; i < dt.Rows.Count; i++)
                    {
                        var r = dt.Rows[i];

                        //var thread = (int)r[SqlConstants.Tag.ThreadId] <= 0 ?
                        //    string.Empty :
                        //    string.Format("<h3 class=\"prop_thread\"><a href=\"{0}\" title=\"{2}\">{1}</a></h3>",
                        //    PresentationUtils.UrlBuilder.BuildThreadUrl(
                        //        (int)r[SqlConstants.Tag.ThreadId],
                        //        r[SqlConstants.Tag.ThreadName].ToString()),
                        //    r[SqlConstants.Tag.ThreadName],
                        //    PresentationUtils.EnCode(r[SqlConstants.Tag.ThreadName].ToString()));


                        var sapo = string.IsNullOrEmpty(r[SqlConstants.NewsPublish.Sapo].ToString()) ||
                                   i >= TopFormalizedItem
                                       ? string.Empty
                                       : string.Format("<p>{0}</p>",
                                                       ToSubString(r[SqlConstants.NewsPublish.Sapo].ToString(), 140));

                        var avatar = i > 0
                            ? PresentationUtils.Photo.BuildNewsAvatar(r, ImageWidth, ImageHeight)
                            : PresentationUtils.Photo.BuildNewsAvatar(r, ImageWidth2, ImageHeight2);

                        sb.AppendFormat(
                            "<li>{4}<div class=\"scope clearfix\"><h2><a href=\"{0}\" title=\"{2}\" class=\"prop_title\">{1}</a></h2>{3}</div></li>",
                            r[SqlConstants.NewsPublish.Url],
                            r[SqlConstants.NewsPublish.Title].ToString(),
                            //i == 0 ? r[SqlConstants.NewsPublish.Title].ToString() : ToSubString(r[SqlConstants.NewsPublish.Title].ToString(), 40),
                            r[SqlConstants.NewsPublish.EncodedTitle],
                            sapo,
                            avatar);
                    }
                    ltrData.Text = sb.ToString();
                    return true;

                }
            }
            catch (Exception ex)
            {
                ErrorManager.Log(ex);
            }
            return false;
        }

        private string ToSubString(string p, int length)
        {
            if (string.IsNullOrEmpty(p) || p.Length <= length)
                return p;
            p = p.Substring(0, Math.Min(p.Length, length));
            p = p.Substring(0, p.LastIndexOf(' '));
            return string.Format("{0}...", p);
        }


    }
}